clear,clc
numIter =10;
conf = 3;
confidence_percent = conf;


path_origdata = ['/home/liang/Projects/ASD_QC/DataProcessed/rest'];
ListPath = '/home/liang/Projects/ASD_QC/Lists/';
outputDIR = ['/home/liang/Projects/ASD_QC/Results/code_result'];

SIDs = textread([ListPath '/censored_sub_list_ASD_OHSU.txt'],'%s');

combineLeftRight = 1;
load Variability_FS4


Prior_Variability = ([lh; rh])'; %ones(1, 2562*2);%Prior_Variability:0.5~0.8
Prior_SNR= ones(1, 2562*2);
% Prior_SNR = ([lh_snr;rh_snr])'; %5.1696~62.6363



%%%%%%%%%%%%%%%%%%%%



for s = 1:length(SIDs)

     sid = SIDs{s}
     lhData=[];
     rhData=[];


     Files_LH = dir([path_origdata '/' sid '/surf/lh.*sm6_fsaverage4.nii.gz']);
     Files_RH = dir([path_origdata '/' sid '/surf/rh.*sm6_fsaverage4.nii.gz']);


     for e = 1:length(Files_LH)

          filename_bold_lh = [Files_LH(e).name];
          filename_bold_rh = [Files_RH(e).name];

          fullfilename_bold_lh = [path_origdata '/'  sid '/surf/' filename_bold_lh];
          fullfilename_bold_rh = [path_origdata '/' sid '/surf/' filename_bold_rh];

          lh_hdr = MRIread(fullfilename_bold_lh);
          lhData_raw = reshape(lh_hdr.vol,[size(lh_hdr.vol,1)*size(lh_hdr.vol,2)*size(lh_hdr.vol,3), size(lh_hdr.vol,4)]);

          rh_hdr = MRIread(fullfilename_bold_rh);
          rhData_raw = reshape(rh_hdr.vol,[size(rh_hdr.vol,1)*size(rh_hdr.vol,2)*size(rh_hdr.vol,3), size(rh_hdr.vol,4)]);

%         lhData_run= butterfilt_surf(lhData_raw, 1/0.72);
%         rhData_run= butterfilt_surf(rhData_raw, 1/0.72);
%
          lhData_run = lhData_raw;
          rhData_run = rhData_raw;
          lhData = [lhData lhData_run];
          rhData = [rhData rhData_run];
      end


      numSegs =1;
      for seg =1:numSegs

%         segDIR = [outputDIR, '/seg', num2str(seg)];
%         eval(['!mkdir ', segDIR]);
          segDIR = outputDIR;

          len = size(lhData,2)/numSegs;

          %Func_IterativeParcellation_TillConverge(lhData(:,(seg-1)*len+1:seg*len), rhData(:,(seg-1)*len+1:seg*len), numIter, con, segDIR, sid, combineLeftRight, Prior_Variability, Prior_SNR)
          Func_IterativeParcellation(lhData(:,(seg-1)*len+1:seg*len), rhData(:,(seg-1)*len+1:seg*len), numIter, conf, segDIR, sid, combineLeftRight, Prior_Variability, Prior_SNR );

      end



        %       voteMembership(outputDIR, sid, numSegs)






end
